package com.mxwl.cbg.common.Extract;

import java.util.*;

public class BaseExtract {
    // 装备特性对照表
    private static final Map<String, String> TRAIT_MAP = new HashMap<>();
    static {
        TRAIT_MAP.put("1", "物理暴击几率");
        TRAIT_MAP.put("2", "法术暴击几率");
        TRAIT_MAP.put("3", "物理暴击伤害");
        TRAIT_MAP.put("4", "法术暴击伤害");
        TRAIT_MAP.put("5", "治疗能力");
        TRAIT_MAP.put("6", "封印命中率");
        TRAIT_MAP.put("7", "抵抗封印命中率");
        TRAIT_MAP.put("8", "穿刺效果");
        TRAIT_MAP.put("9", "格挡物理伤害");
        TRAIT_MAP.put("10", "魔法回复");
        TRAIT_MAP.put("11", "法术伤害减免效果");
    }

    private static final Set<String> COMMON_STYLES = new HashSet<>(Arrays.asList(
            "#R", "#G", "#B", "#K", "#Y", "#W"
    ));

    private static final Set<String> SPECIAL_STYLES = new HashSet<>(Arrays.asList(
            "#b", "#u"
    ));

    private static final Set<String> ALL_STYLES = new HashSet<>();

    static {
        ALL_STYLES.addAll(COMMON_STYLES);
        ALL_STYLES.addAll(SPECIAL_STYLES);
        ALL_STYLES.add("#n"); // 添加重置样式
    }

    public static String parseEquipmentText(String input) {
        StringBuilder result = new StringBuilder();
        int length = input.length();
        int i = 0;

        while (i < length) {
            char current = input.charAt(i);

            // 处理样式标记
            if (current == '#' && i + 1 < length) {
                char nextChar = input.charAt(i + 1);

                // 处理转义的 ##
                if (nextChar == '#') {
                    result.append('#');
                    i += 2;
                    continue;
                }

                // 处理换行 #r
                if (nextChar == 'r') {
                    result.append('\n');
                    i += 2;
                    continue;
                }

                // 处理自定义颜色 #cXXXXXX
                if (nextChar == 'c' && i + 8 <= length) {
                    i += 8; // 跳过整个颜色标记
                    continue;
                }

                // 检查是否是样式标记 (# + 1字符)
                if (i + 2 <= length) {
                    String marker = input.substring(i, i + 2);

                    // 处理已知样式标记
                    if (ALL_STYLES.contains(marker)) {
                        i += 2; // 跳过标记
                        continue;
                    }

                    // 处理重置样式 #n
                    if ("#n".equals(marker)) {
                        i += 2;
                        continue;
                    }
                }

                // 未知标记，保留#字符继续处理
                result.append(current);
                i++;
                continue;
            }

            // 普通字符直接添加
            result.append(current);
            i++;
        }

        return result.toString().trim();
    }
}
